package cn.iocoder.yudao.module.eval.service.score;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.eval.controller.admin.score.vo.EvalScoreRecordPageReqVO;
import cn.iocoder.yudao.module.eval.controller.admin.score.vo.EvalScoreRecordSaveReqVO;
import cn.iocoder.yudao.module.eval.dal.dataobject.score.EvalScoreRecordDO;
import cn.iocoder.yudao.module.eval.dal.mysql.score.EvalScoreRecordMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.eval.enums.ErrorCodeConstants.*;

/**
 * 评分记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class EvalScoreRecordServiceImpl implements EvalScoreRecordService {

    @Resource
    private EvalScoreRecordMapper scoreRecordMapper;

    @Override
    public Long createScoreRecord(EvalScoreRecordSaveReqVO createReqVO) {
        // 校验评分记录是否重复
        validateScoreRecordDuplicate(createReqVO.getPlanId(), createReqVO.getSubjectId(), 
                createReqVO.getCandidateId(), createReqVO.getEvaluatorId(), null);

        // 插入
        EvalScoreRecordDO scoreRecord = BeanUtils.toBean(createReqVO, EvalScoreRecordDO.class);
        scoreRecord.setSubmitTime(LocalDateTime.now());
        scoreRecordMapper.insert(scoreRecord);
        // 返回
        return scoreRecord.getId();
    }

    @Override
    public void updateScoreRecord(EvalScoreRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateScoreRecordExists(updateReqVO.getId());
        // 校验评分记录是否重复
        validateScoreRecordDuplicate(updateReqVO.getPlanId(), updateReqVO.getSubjectId(), 
                updateReqVO.getCandidateId(), updateReqVO.getEvaluatorId(), updateReqVO.getId());

        // 更新
        EvalScoreRecordDO updateObj = BeanUtils.toBean(updateReqVO, EvalScoreRecordDO.class);
        scoreRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteScoreRecord(Long id) {
        // 校验存在
        validateScoreRecordExists(id);
        // 删除
        scoreRecordMapper.deleteById(id);
    }

    private void validateScoreRecordExists(Long id) {
        if (scoreRecordMapper.selectById(id) == null) {
            throw exception(SCORE_RECORD_NOT_EXISTS);
        }
    }

    private void validateScoreRecordDuplicate(Long planId, Long subjectId, Long candidateId, Long evaluatorId, Long id) {
        EvalScoreRecordDO scoreRecord = scoreRecordMapper.selectByPlanSubjectCandidateEvaluator(planId, subjectId, candidateId, evaluatorId);
        if (scoreRecord == null) {
            return;
        }
        // 如果 id 为空，说明不用比较是否为相同 id 的记录
        if (id == null) {
            throw exception(SCORE_RECORD_DUPLICATE);
        }
        if (!scoreRecord.getId().equals(id)) {
            throw exception(SCORE_RECORD_DUPLICATE);
        }
    }

    @Override
    public EvalScoreRecordDO getScoreRecord(Long id) {
        return scoreRecordMapper.selectById(id);
    }

    @Override
    public PageResult<EvalScoreRecordDO> getScoreRecordPage(EvalScoreRecordPageReqVO pageReqVO) {
        return scoreRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<EvalScoreRecordDO> getScoreRecordList(EvalScoreRecordPageReqVO exportReqVO) {
        return scoreRecordMapper.selectList(exportReqVO);
    }

    @Override
    public List<EvalScoreRecordDO> getScoreRecordListByPlanAndSubject(Long planId, Long subjectId) {
        return scoreRecordMapper.selectListByPlanIdAndSubjectId(planId, subjectId);
    }

    @Override
    public List<EvalScoreRecordDO> getScoreRecordListByPlanAndCandidate(Long planId, Long candidateId) {
        return scoreRecordMapper.selectListByPlanIdAndCandidateId(planId, candidateId);
    }

    @Override
    public void batchCreateScoreRecords(List<EvalScoreRecordSaveReqVO> scoreRecords) {
        for (EvalScoreRecordSaveReqVO scoreRecord : scoreRecords) {
            createScoreRecord(scoreRecord);
        }
    }

}
